package com.jdz.service.impl;

import com.jdz.common.ServerResponse;
import com.jdz.dao.ProjectDetailsMapper;
import com.jdz.dao.RaceTypeMapper;
import com.jdz.pojo.Institute;
import com.jdz.pojo.ProjectDetails;
import com.jdz.pojo.RaceType;
import com.jdz.service.IRaceTypeService;
import com.jdz.util.DateTimeUtil;
import com.jdz.vo.RaceTypeVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import sun.plugin2.os.windows.SECURITY_ATTRIBUTES;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sun
 **/
@Transactional
@Service("IRaceTypeService")
public class RaceTypeServiceImpl implements IRaceTypeService {

    @Autowired
    RaceTypeMapper raceTypeMapper;
    @Autowired
    ProjectDetailsMapper projectDetailsMapper;

    //backend
    private Logger logger = LoggerFactory.getLogger(RaceTypeServiceImpl.class);

    public ServerResponse addRaceType(String racetype,int parentId,String startTime,String stopTime) {

        if(StringUtils.isBlank(racetype)) {
            return ServerResponse.createByErrorMessage("添加品类参数错误");
        }
        RaceType raceType = raceTypeMapper.selectByRaceType(racetype);
        if(raceType!=null){
            return ServerResponse.createByErrorMessage("该类型已存在");
        }

        if(parentId==0){
            RaceType race = new RaceType();
            race.setRacetypeno(null);
            race.setParentId(parentId);
            race.setRacetype(racetype);
            race.setCreattime(new Date());

            int rowCount = raceTypeMapper.insertSelective(race);
            if(rowCount > 0){
                return ServerResponse.createBySuccessMessage("添加品类成功");
            }
            return ServerResponse.createByErrorMessage("添加品类失败");
        }

        Date startTimeDate;
        Date stopTimeDate;


        try {

            if(StringUtils.isBlank(stopTime)||StringUtils.isBlank(startTime))
                return ServerResponse.createByErrorMessage("参数错误");
            else
                startTimeDate = DateTimeUtil.strToDate(startTime,"yyyy-MM-dd");
                stopTimeDate = DateTimeUtil.strToDate(stopTime,"yyyy-MM-dd");

        }catch (Exception e){
            return ServerResponse.createByErrorMessage("输入结束时间参数格式错误");
        }


        RaceType race = new RaceType();
        race.setRacetypeno(null);
        race.setParentId(parentId);
        race.setRacetype(racetype);
        race.setStarttime(startTimeDate);
        race.setStoptime(stopTimeDate);
        race.setCreattime(new Date());

        int rowCount = raceTypeMapper.insertSelective(race);
        if(rowCount > 0){
            return ServerResponse.createBySuccessMessage("添加品类成功");
        }
        return ServerResponse.createByErrorMessage("添加品类失败");

    }
    public ServerResponse updateRaceType(Integer racetypeno,String racetype){
        if(racetypeno == null || StringUtils.isBlank(racetype)){
            return ServerResponse.createByErrorMessage("更新品类参数错误");
        }


        RaceType raceTypeRow = raceTypeMapper.selectByPrimaryKey(racetypeno);
        if(raceTypeRow==null){
            return ServerResponse.createByErrorMessage("没有此类型id");
        }

        List<ProjectDetails> projectDetailsList = projectDetailsMapper.selectByProjectRaceType(raceTypeRow.getRacetype());

        for(ProjectDetails projectDetailsItem : projectDetailsList){
            ProjectDetails projectDetails = new ProjectDetails();
            projectDetails.setProjectno(projectDetailsItem.getProjectno());
            projectDetails.setProjecttype(racetype);
            int i = projectDetailsMapper.updateByPrimaryKeySelective(projectDetails);
            if(i<=0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("更新品类参数错误");
            }
        }

        RaceType race = new RaceType();
        race.setRacetypeno(racetypeno);
        race.setRacetype(racetype);

        int rowCount = raceTypeMapper.updateByPrimaryKeySelective(race);
        if(rowCount <= 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ServerResponse.createByErrorMessage("更新品类名字失败");
        }
        return ServerResponse.createBySuccessMessage("更新品类名字成功");
    }

    public ServerResponse deleteRaceType(Integer racetypeno){
        if(racetypeno == null || racetypeno==0){
            return ServerResponse.createByErrorMessage("删除品类参数错误");
        }


        int rowCount = raceTypeMapper.deleteByPrimaryKey(racetypeno);

        List<RaceType> raceTypeList = raceTypeMapper.selectCategoryByParentId(racetypeno);
        for(RaceType raceTypeItem : raceTypeList){
            int r = raceTypeMapper.deleteByPrimaryKey(raceTypeItem.getRacetypeno());
            if(r<=0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("删除品类名字失败");
            }
        }

        if(rowCount > 0){
            return ServerResponse.createBySuccessMessage("删除品类名字成功");
        }
        return ServerResponse.createByErrorMessage("删除品类名字失败");
    }



    public ServerResponse<List<RaceTypeVo>> getParallelRaceType(Integer parentId){
        List<RaceType> raceTypeList = raceTypeMapper.selectCategoryByParentId(parentId);
        if(CollectionUtils.isEmpty(raceTypeList)){
            logger.info("未找到当前分类的子分类");
        }

        List<RaceTypeVo> raceTypeVoArrayList = new ArrayList<>();

        if(!CollectionUtils.isEmpty(raceTypeList)){
            for(RaceType raceTypeItem : raceTypeList){
                RaceTypeVo raceTypeVo = new RaceTypeVo();
                raceTypeVo.setRacetypeno(raceTypeItem.getRacetypeno());
                raceTypeVo.setRacetype(raceTypeItem.getRacetype());
                raceTypeVo.setParentId(raceTypeItem.getParentId());
                raceTypeVo.setStarttime(DateTimeUtil.dateToStr(raceTypeItem.getStarttime(),"yyyy-MM-dd"));
                raceTypeVo.setStoptime(DateTimeUtil.dateToStr(raceTypeItem.getStoptime(),"yyyy-MM-dd"));
                raceTypeVo.setStatus(raceTypeItem.getStatus());
                raceTypeVo.setCreattime(DateTimeUtil.dateToStr(raceTypeItem.getCreattime()));
                raceTypeVoArrayList.add(raceTypeVo);
            }
        }


        return ServerResponse.createBySuccess(raceTypeVoArrayList);
    }


    public ServerResponse getAllParallelRaceType(){
        List<RaceType> raceTypeList = raceTypeMapper.selectAll();
        if(CollectionUtils.isEmpty(raceTypeList)){
            logger.info("未找到当前分类的子分类");
        }
        return ServerResponse.createBySuccess(raceTypeList);
    }


    public ServerResponse startDeclaring(List<Integer> racetypenoList){

        for(Integer racetypenoItem : racetypenoList){
            RaceType raceType = new RaceType();
            raceType.setRacetypeno(racetypenoItem);
            raceType.setStatus(0);
            int row =  raceTypeMapper.updateByPrimaryKeySelective(raceType);

            if(row<=0){

                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("开始申报失败");
            }
       }

        return ServerResponse.createBySuccessMessage("开始申报成功");
    }

    public ServerResponse stopDeclaring(List<Integer> racetypenoList){

        for(Integer racetypenoItem : racetypenoList){
            RaceType raceType = new RaceType();
            raceType.setRacetypeno(racetypenoItem);
            raceType.setStatus(1);
            int row =  raceTypeMapper.updateByPrimaryKeySelective(raceType);

            if(row<=0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("关闭申报失败");
            }
        }

        return ServerResponse.createBySuccessMessage("关闭申报成功");
    }

    public ServerResponse setStartTime(List<Integer> racetypeno,String startTime){


        Date startTimeDate;


        try {

            if(StringUtils.isBlank(startTime))
                return ServerResponse.createByErrorMessage("参数错误");
            else
                startTimeDate = DateTimeUtil.dateMin(startTime);

        }catch (Exception e){
            return ServerResponse.createByErrorMessage("时间参数格式错误");
        }



        for(Integer raceTypenoItem : racetypeno){

            RaceType raceType = new RaceType();

            raceType.setRacetypeno(raceTypenoItem);
            raceType.setStarttime(startTimeDate);

            int row = raceTypeMapper.updateByPrimaryKeySelective(raceType);

            if(row<=0 || raceType.getStarttime()==null){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("设置开始申报失败");
            }
        }

        return ServerResponse.createBySuccessMessage("设置开始申报成功");
   }

    public ServerResponse setStopTime(List<Integer> racetypeno,String stopTime){


        Date stopTimeDate;


        try {

            if(StringUtils.isBlank(stopTime))
                return ServerResponse.createByErrorMessage("参数错误");
            else
                stopTimeDate = DateTimeUtil.dateMax(stopTime);

        }catch (Exception e){
            return ServerResponse.createByErrorMessage("时间参数格式错误");
        }

        for(Integer raceTypenoItem : racetypeno){

            RaceType raceType = new RaceType();

            raceType.setRacetypeno(raceTypenoItem);
            raceType.setStoptime(stopTimeDate);

            int row = raceTypeMapper.updateByPrimaryKeySelective(raceType);

            if(row<=0 || raceType.getStoptime()==null){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ServerResponse.createByErrorMessage("设置结束申报失败");
            }
        }

        return ServerResponse.createBySuccessMessage("设置结束申报成功");
    }

    //protal
}
